using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using net_work.Common.Request;
using net_work.Common.Result;
using net_work.Data.DTO.Hmw;
using net_work.IService.Hmw;

namespace net_work.Controllers.Hmw;

[Route("api/[controller]")]
[ApiController]
public class BankAnswerController(ILogger<BankAnswerController> logger, IBankAnswerService bankAnswerService)
    : ControllerBase
{
    /// <summary>
    /// 获取题库答案列表
    /// </summary>
    /// <param name="request">分页请求实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("list")]
    [Authorize]
    public async Task<IActionResult> ListBankAnswers([FromBody] PagedRequest request)
    {
        logger.LogInformation("获取题库答案列表");
        var result = await bankAnswerService.GetBankAnswersPagedAsync(request);
        return AjaxResult.Success(result);
    }

    /// <summary>
    /// 获取题库答案信息
    /// </summary>
    /// <param name="bankAnswerId">题库答案ID</param>
    /// <returns>操作结果</returns>
    [HttpGet("get")]
    [Authorize]
    public async Task<IActionResult> GetBankAnswerById([FromQuery] long bankAnswerId)
    {
        logger.LogInformation("获取题库答案信息");
        return await bankAnswerService.SelectByIdAsync(bankAnswerId) is { } bankAnswer
            ? AjaxResult.Success(bankAnswer)
            : AjaxResult.Error();
    }

    /// <summary>
    /// 新增题库答案
    /// </summary>
    /// <param name="bankAnswerAddRequest">新增题库答案实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("add")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> AddBankAnswer([FromBody] BankAnswerAddRequest bankAnswerAddRequest)
    {
        logger.LogInformation("新增题库答案");
        var bankAnswerId = await bankAnswerService.InsertBankAnswer(bankAnswerAddRequest);
        return bankAnswerId > 0
            ? AjaxResult.Success(new { bankAnswerId })
            : AjaxResult.Error();
    }

    /// <summary>
    /// 修改题库答案
    /// </summary>
    /// <param name="bankAnswerUpdateRequest">修改题库答案实体</param>
    /// <returns>操作结果</returns>
    [HttpPost("update")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> UpdateBankAnswer([FromBody] BankAnswerUpdateRequest bankAnswerUpdateRequest)
    {
        logger.LogInformation("修改题库答案");
        return await bankAnswerService.UpdateBankAnswer(bankAnswerUpdateRequest) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }

    /// <summary>
    /// 删除题库答案
    /// </summary>
    /// <param name="ids">题库答案ID</param>
    /// <returns>操作结果</returns>
    [HttpPost("delete")]
    [Authorize(Roles = "ADMIN,SECRET,TEACHER")]
    public async Task<IActionResult> DeleteBankAnswer([FromBody] List<long> ids)
    {
        logger.LogInformation("删除题库答案");
        return await bankAnswerService.DeleteBankAnswerByIds(ids) > 0
            ? AjaxResult.Success()
            : AjaxResult.Error();
    }
}